#include "contourwindow.hpp"

ContourWindow::ContourWindow(Contour* con,MainWindow *main){
    type=CONTOUR;

    principalwindow = main;

    //interface load
    Glib::RefPtr<Gnome::Glade::Xml> Widgetstree = Gnome::Glade::Xml::create("contour.glade");

    Widgetstree->get_widget_derived("window", window);
    Widgetstree->get_widget("menusave", menusave);
    Widgetstree->get_widget("menuopen", menuopen);
    Widgetstree->get_widget("menutest", menutest);
    Widgetstree->get_widget("menusaveimage", menusaveimage);


    Widgetstree->get_widget("image", image);

    //signal linkage
    menusave->signal_activate().connect(sigc::mem_fun(*this, &ContourWindow::save));
    menuopen->signal_activate().connect(sigc::mem_fun(*this, &ContourWindow::open));
    menusaveimage->signal_activate().connect(sigc::mem_fun(*this, &ContourWindow::saveimage));
    menutest->signal_activate().connect(sigc::mem_fun(*this, &ContourWindow::test));

    window->signal_delete_event().connect(sigc::mem_fun(*this, &ContourWindow::close));

    //Initializations
    contour=con;
    interfaceimage=imagetoPixbuf(contour->img);
    image->set(*interfaceimage);

    id=principalwindow->emptyid;
    principalwindow->emptyid++;

    string title("ID: ");
    std::stringstream ss;
    ss << id;
    title+=ss.str();
    window->set_title(title);
    window->show_all();
}

ContourWindow::~ContourWindow(){
    window->destroy_();
    delete window;
    delete interfaceimage;
    delete contour;
}

bool ContourWindow::close(GdkEventAny *){
    principalwindow->closethisObject(this);
    return true;
}
void ContourWindow::open(){

}
void ContourWindow::save(){

}

void ContourWindow::test(){
    Contour* newcont=median(contour,17);
    Contour* interpolated=linearinterpolation(newcont);
    principalwindow->objectsopen.push_back(new ContourWindow(interpolated,principalwindow));
    principalwindow->updateimagetree();
}

void ContourWindow::saveimage(){
    Gtk::FileChooserDialog dialog("Please choose a file",
          Gtk::FILE_CHOOSER_ACTION_SAVE);
    dialog.set_transient_for(*(this->window));

    //Add response buttons the the dialog:
    dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
    dialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK);

      //Show the dialog and wait for a user response:
    int result = dialog.run();

    //Handle the response:
    switch(result)
    {
        case(Gtk::RESPONSE_OK):
        {
            (*interfaceimage)->save(dialog.get_filename(),"png");
            break;
        }
        case(Gtk::RESPONSE_CANCEL):
        {
            break;
        }
        default:
        {
            break;
        }
  }
}
